﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Pulsar
{
    public partial class Options : Form
    {
        ToolStripLabel optionsStatusLabel;

        public Options()
        {
            InitializeComponent();
            this.CenterToScreen();

            textBox1.Text = PulsarGlobalOptions.ParallelPortAddress.ToString();
            comboBox1.SelectedItem = PulsarGlobalOptions.ComPortName;
            comboBox2.SelectedItem = PulsarGlobalOptions.ComPortBaudRate.ToString();
            if (PulsarGlobalOptions.Tagger is PulsarPortParallel)
                radioButton1.Checked = true;
            else
                if (PulsarGlobalOptions.Tagger is PulsarSerialPort)
                    radioButton2.Checked = true;
                else
                    if (PulsarGlobalOptions.Tagger is BulkTagger)
                        radioButton3.Checked = true;

            optionsStatusLabel = new ToolStripLabel();
            statusStrip1.Items.Add(optionsStatusLabel);
            optionsStatusLabel.Text = "Ready!";
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            PulsarGlobalOptions.ParallelPortAddress = Convert.ToInt32(textBox1.Text);
            PulsarGlobalOptions.ComPortName = (string)comboBox1.SelectedItem;
            PulsarGlobalOptions.ComPortBaudRate = Convert.ToInt32(comboBox2.SelectedItem);

            if (radioButton1.Checked)
                PulsarGlobalOptions.Tagger = new PulsarPortParallel();
            else
                if (radioButton2.Checked)
                    PulsarGlobalOptions.Tagger = new PulsarSerialPort();
                else
                    if (radioButton3.Checked)
                        PulsarGlobalOptions.Tagger = new BulkTagger();

            this.Close();
        }

        private void buttonCOMAutoDetect_Click(object sender, EventArgs e)
        {
            optionsStatusLabel.Text = "Searching for virtual COM ports . . .";
            statusStrip1.Refresh();

            string[] devices = PulsarSerialPort.GetUSBCOMDevices();

            if (devices.Length > 0)
            {
                comboBox1.SelectedItem = devices[0];
                optionsStatusLabel.Text = devices.Length + " port(s) found, first one has been selected";
            }
            else
            {
                optionsStatusLabel.Text = "No virtual COM ports found!";
            }

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] devices = PulsarSerialPort.GetAllExisting();

            if (devices.Length > 0)
            {
                comboBox1.Items.Clear();

                comboBox1.Items.AddRange(devices);

                comboBox1.SelectedIndex = 0;

                optionsStatusLabel.Text = devices.Length + " port(s) found, first one has been selected";

             
            }
            else
            {
                optionsStatusLabel.Text = "No COM ports found.";
            }
        }
    }
}
